package com.vortex.cloud.zhsw.xinyang.annotation;

import com.vortex.cloud.zhsw.xinyang.enums.IBaseEnum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.ArrayList;
import java.util.List;

public class StatusValueValidator implements ConstraintValidator<StatusValue, Integer> {

    /**
     * 使用set在initialize获得参数信息，在isValid方法中校验，成功true，失败false
     */
    List<Integer> containsValues = new ArrayList<>();

    @Override
    public void initialize(StatusValue constraintAnnotation) {
        containsValues.addAll(IBaseEnum.getValues(constraintAnnotation.baseEnum()));
    }

    /**
     * 校验是否匹配
     *
     * @param value                      需要校验的值
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        return containsValues.contains(value);
    }
}
